<?php

namespace app\admin\controller\admin;

use app\common\controller\AdminController;
use app\common\services\annotation\ControllerAnnotation;
use app\common\services\annotation\NodeAnnotation;
use support\Request;
use support\Response;
use think\Exception;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="activity_prize")
 */
class ActivityPrizeController extends AdminController
{

    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\ActivityPrize();
    }

    /**
     * @NodeAnnotation(title="列表")
     */
    public function index(Request $request): Response
    {
        $activity_id = $request->get('activity_id');
        if (!$request->isAjax()) {
            return $this->fetch('', ['activityId' => $activity_id]);
        }
        if ($request->input('selectFields')) {
            return $this->selectList();
        }
        list($page, $limit) = $this->buildTableParams();
        $where[] = ['activity_id', '=', $activity_id];
        $count = $this->model->where($where)->count();
        $list  = $this->model->where($where)->order($this->order)->page($page, $limit)->select()->toArray();
        foreach ($list as &$value) {
            if (!empty($value['stock'])) {
                $value['actual_odds'] = round(($value['odds'] / Db::name('activity_prize')->where(['activity_id' => $activity_id])->where('stock', '>', 0)->sum('odds')) * 100, 2);
            } else {
                $value['actual_odds'] = 0;
            }
        }
        $data  = [
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list,
        ];

        return json($data);
    }

    /**
     * @NodeAnnotation(title="添加")
     */
    public function add(Request $request): Response
    {
        if ($request->isAjax()) {
            $post = $request->post();
            try {
                $post['odds'] = ((int)($post['odds']) >= 100) ? 100 : $post['odds'] ;
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                return $this->error('保存失败:' . $e->getMessage());
            }
            return $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $activity_id = $request->get('activity_id');
        return $this->fetch('', ['activityId' => $activity_id]);
    }

    /**
     * @NodeAnnotation(title="编辑")
     */
    public function edit(Request $request)
    {
        $id  = (int)$request->input('id');
        $row = $this->model->find($id);
        if (empty($row)) return $this->error('数据不存在');
        if ($request->isAjax()) {
            $post = $request->post();
            try {
                $post['odds'] = ((int)($post['odds']) >= 100) ? 100 : $post['odds'] ;
                $save = $row->save($post);
            } catch (\PDOException|\Exception $e) {
                return $this->error('保存失败:' . $e->getMessage());
            }
            return $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign(compact('row'));
        return $this->fetch();
    }

}
